<?php include '_init_.php';?>
<?php include 'page_header.php';?>
<?php
$id = isset($_REQUEST['id'])?intval($_REQUEST['id']):0;
if($id < 1)
	exit("Please specify ID");
$orderManager = Order_Manager::getOrderManager($_REQUEST['id']);
if(!$orderManager)
	exit('Data not exist anymore');
if($orderManager->getStatus() != Order_Manager::$STATUS_CREATING)
{
	exit("Order doesn't editable");
}
$orderManager->setEditing();

$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
if($act == 'save')
{
	$orderManager->setPayInfo($_REQUEST);
	$orderManager->persistOrderInfo();
	$orderManager->setNotEditing();
	try
	{
	$orderManager->injectOrderMainIndex();
	header('Location:index.php');
	exit();
	}catch(Exception $ex)
	{
		if($ex->getMessage() == 'order number taken')
		{
			echo "<script language='javascript'>alert('預約單號已存在，訂單號不能够重復');document.location=add_costb.php?id=".$orderManager->getId()."</script>";
			exit();
		}else{
			ob_clean();
			echo $ex->getMessage();
			exit();
		}
	}
}
$orderManager->init();
$pageInfo = $orderManager->getPriceInfo();
$orderInfo = $orderManager->getInfo();
?>
<script language="javascript">
$(function(){
	$('input[name=total]').bind('change',function(){
		auto_calculate_need_pay();
	});
	
	$('input[name=pre_pay]').bind('change',function(){
		auto_calculate_need_pay();
	});
});
function auto_calculate_need_pay()
{
	var priceTotal = parseFloat($('input[name=total]').val());
		var pricePrePay = parseFloat($('input[name=pre_pay]').val());
		if(!isNaN(priceTotal) && !isNaN(priceTotal))
		{
			$('input[name=need_pay]').val(priceTotal - pricePrePay);
		}else{
			$('input[name=need_pay]').val('');
		}
}
</script>
<div id="main"><!-- start #main //-->
<form method="post" action='add-cose.php'>
<input type="hidden" name="act" value="save"/>
<input type="hidden" name="id" value="<?php echo $orderManager->getId();?>"/>
<div id="tita">
  <div class="tita-1">
    <h3>會計項目</h3>
    <img src="images/point-1.gif" width="18" height="18" /> 
    <h4>款項相關</h4>
    </div>
  <div class="tita-2">預約單號 No.<?php echo Util::html_safe($orderInfo['number']);?></div>
  </div>
  <div id="mainf" class="clearfix"><!-- start #mainf //-->

    <div class="mainf-3">
      <ul>
     <li class="mainf-1k">消費總金額<input name="total" type="text"  class="maine-keyinj _validate_form_float" value="<?php echo $pageInfo['total'];?>"/></li>
      <li class="mainf-1k">預約訂金<input name="pre_pay" type="text"  class="maine-keyinj _validate_form_float" value="<?php echo $pageInfo['pre_pay'];?>"/></li>
      <li class="mainf-1k">剩餘款項<input name="need_pay" type="text"  class="maine-keyinj _validate_form_float" value="<?php echo $pageInfo['need_pay'];?>"/></li>
      </ul>
   </div>
  </div><!-- start #mainf //-->
    
     
  
    <div id="paged"><!-- start #pagec //-->
   <img width="77" height="29" src="images/btn-prev.gif"></a><a href="#" onclick="document.forms[0].submit();return false;"><img src="images/btn-ok.gif" width="77" height="29" /></a></div><!-- end #paged //-->
</div><!-- end #tita //-->
</form>
</div><!-- end #main //-->
<?php include 'page_footer.php';?>